package com.tg.app.activity.device.binding;

import android.text.TextUtils;

import com.tange.feature.data.structure.DeviceTypeBean;
import com.tg.app.TGSdkHelper;
import com.tg.appcommon.android.TGLog;
import com.tange.core.backend.service.http.ClientObserver;
import com.tange.core.backend.service.api.CoreBackendService;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 通过uuid，来查询设备类型
 *
 * 注意：不要把业务逻辑放到这里
 *
 * @author allenfeng
 */
public class DeviceTypeQuery {

    private static final String TAG = "DeviceTypeQuery";

    public static final int SERVER_ERROR = -100;
    public static final int APP_ERROR = -200;

    public interface Callback {

        /**
         * 查询到了对应的设备类型
         * @param deviceTypeBean 设备类型信息接口
         */
        void onSuccess(DeviceTypeBean deviceTypeBean);

        /**
         * 后台找不到对应的uuid
         */
        void onUuidNotFound();

        /**
         * 后台查询错误
         *
         * @param errorCode
         * @param errorMsg
         */
        void onError(int errorCode, String errorMsg);
    }

    private DeviceTypeQuery() {

    }

    public static DeviceTypeQuery create() {
        return new DeviceTypeQuery();
    }

    public void request(String uuid, Callback callback) {
        if (callback == null) {
            throw new IllegalArgumentException("Callback callback must not be NULL");
        }

        if (TextUtils.isEmpty(uuid)) {
            callback.onError(APP_ERROR, "uuid is empty");
            return;
        }

        final HashMap<String, String> map = new HashMap<>();
        map.put("uuid", uuid);

        TGHttp.getInstance().getDeviceType(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<DeviceTypeBean>() {
                    @Override
                    protected void onSuccess(final DeviceTypeBean content) {

                        String deviceType = content != null ? content.getDevice_type() : "";
                        TGLog.i(TAG, "onSuccess: deviceType = " + deviceType);
                        if (!TextUtils.isEmpty(deviceType)) {
                            callback.onSuccess(content);
                        } else {
                            callback.onError(SERVER_ERROR, "device type is empty");
                        }
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        TGLog.i(TAG, "onResponseError: errorCode = " + errorCode + " , errorInfo = " + errorInfo);
                        if (errorCode == 400){
                            callback.onUuidNotFound();
                        }else{
                            callback.onError(errorCode, errorInfo);
                        }

                    }

                    @Override
                    protected void onOtherError(String error) {
                        TGLog.i(TAG, "onOtherError: error = " + error);
                        callback.onError(APP_ERROR, error);
                    }
                });
    }
}